package com.niiwoo.civet.user.service.local;

import com.niiwoo.civet.user.dto.UserAndContentDTO;
import com.niiwoo.civet.user.dto.UserAndParamsDTO;
import com.niiwoo.tripod.base.helper.BeanFactoryHelper;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class SmsJobService implements Job {


    private SendSmsLocalService sendSmsLocalService;

    public SmsJobService(){
        this.sendSmsLocalService = BeanFactoryHelper.getBean(SendSmsLocalService.class);
    }

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {

        List<UserAndParamsDTO> userAndParamsList = (List<UserAndParamsDTO>)jobExecutionContext.getJobDetail().getJobDataMap().get("userAndParamsList");
        String templateKey = jobExecutionContext.getJobDetail().getJobDataMap().getString("templateKey");
        List<UserAndContentDTO> userAndContentList = (List<UserAndContentDTO>)jobExecutionContext.getJobDetail().getJobDataMap().get("userAndContentList");


        List<SendSmsLocalService.MobileAndContent> mobileAndContentList = new ArrayList<>();

        //构建短信发送参数对象
        if (!CollectionUtils.isEmpty(userAndParamsList) && !StringUtils.isEmpty(templateKey)) {
            for (UserAndParamsDTO userAndParams : userAndParamsList) {
                SendSmsLocalService.MobileAndContent mobileAndContent = null;
                if (!StringUtils.isEmpty(userAndParams.getMobileNo())) {
                    mobileAndContent = sendSmsLocalService.initSmsSendByMobile(userAndParams.getMobileNo(), templateKey, userAndParams.getParams());
                } else if (!StringUtils.isEmpty(userAndParams.getUserId())) {
                    mobileAndContent = sendSmsLocalService.initSmsSendByMobile(userAndParams.getUserId(), templateKey, userAndParams.getParams());
                }
                mobileAndContentList.add(mobileAndContent);
            }
        } else if (!CollectionUtils.isEmpty(userAndContentList)) {
            for (UserAndContentDTO userAndContent : userAndContentList) {
                SendSmsLocalService.MobileAndContent mobileAndContent = null;
                if (!StringUtils.isEmpty(userAndContent.getMobileNo())) {
                    mobileAndContent = sendSmsLocalService.initSmsSendByMobile(userAndContent.getMobileNo(), userAndContent.getContent());
                } else if (!StringUtils.isEmpty(userAndContent.getUserId())) {
                    mobileAndContent = sendSmsLocalService.initSmsSendByMobile(userAndContent.getUserId(), userAndContent.getContent());
                }
                mobileAndContentList.add(mobileAndContent);
            }
        }


        //循环发送短信
        for (SendSmsLocalService.MobileAndContent mobileAndContent : mobileAndContentList) {
            sendSmsLocalService.doSend(mobileAndContent);
        }
    }
}
